﻿// -----------------------------------------------------------------------
// <copyright company="Christoph van der Fecht - VDsoft">
// Copyright (C) Christoph van der Fecht - VDsoft - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited,
// unless it is communicated and authorized by the copyright owner.
// </copyright>
// -----------------------------------------------------------------------

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System.Collections.Generic;
using System.Windows;

namespace SeriesManager.ViewModel.Options
{
	/// <summary>
	/// View model for the options dialog.
	/// </summary>
	public class OptionsDialogViewModle : ViewModelBase
	{

		#region private commands

		/// <summary>
		/// Command to save all settings.
		/// </summary>
		private RelayCommand saveSettingsCommand = null;

		private RelayCommand<Window> cancelCommand = null;

		#endregion

		/// <summary>
		/// Initializes a new instance of the <see cref="OptionsDialogViewModle"/> class.
		/// </summary>
		public OptionsDialogViewModle()
		{
			if (this.IsInDesignMode)
			{

			}
			else
			{
				this.LoadSettings();
			}
		}

		#region public commands

		/// <summary>
		/// Gets the command to save all settings.
		/// </summary>
		public RelayCommand SaveSettingsCommand
		{
			get
			{
				if (this.saveSettingsCommand == null)
				{
					this.saveSettingsCommand = new RelayCommand(this.SaveSetting);
				}

				return this.saveSettingsCommand;
			}
		}

		/// <summary>
		/// Gets the command to close the window.
		/// </summary>
		public RelayCommand<Window> CancelCommand
		{
			get
			{
				if (this.cancelCommand == null)
				{
					this.cancelCommand = new RelayCommand<Window>(this.CloseWindow);
				}

				return this.cancelCommand;
			}
		}

		#endregion

		/// <summary>
		/// Saves the settings of all options parts.
		/// </summary>
		public void SaveSetting()
		{
			foreach (KeyValuePair<string, OptionsPart> pair in ViewModelLocator.Options)
			{
				pair.Value.SaveSettings();
			}

			Properties.Settings.Default.Save();
		}

		/// <summary>
		/// Loads the settings of all options parts.
		/// </summary>
		public void LoadSettings()
		{
			foreach (KeyValuePair<string, OptionsPart> pair in ViewModelLocator.Options)
			{
				pair.Value.LoadSettings();
			}
		}

		/// <summary>
		/// Closes the provided window.
		/// </summary>
		/// <param name="window">Window to close.</param>
		private void CloseWindow(Window window)
		{
			if (window != null)
			{
				window.Close();
			}
		}
	}
}
